﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using SalePartial.BLL.SAServiceRef;
using SalePartial.BLL;
using Telerik.Web.UI;

public partial class SAClientWeb_EditCategoryFrm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                RadDockLayout1.Visible = false;
                SABLL.ShowInfoMsg(this, "对不起，您权限不够！");
            }
             
               
            EditCategoryFrm_Load();
        }

    }

    protected void button_AddRootCategory_Click(object sender, EventArgs e)
    {
        //add to UI.
        Guid newCateId = Guid.NewGuid();
        RadTreeNode newRootNode = new RadTreeNode();
        newRootNode.Text = "新根节点";
        this.treeView_category.Nodes.Add(newRootNode);

        //add to database.
        CategoryInfo newCateInfo = new CategoryInfo();
        newCateInfo.Name = newRootNode.Text;
        newCateInfo.Id = newCateId;
        newCateInfo.ParentId = Guid.Empty;
        newCateInfo.Idx = newRootNode.Index;

        newRootNode.Value = newCateInfo.Id.ToString();
        SABLL.AddCategory(newCateInfo);

        //begin edit
        //newRootNode.BeginEdit();
        //this.treeView_category.Refresh();
        //this.treeView_category.SelectedNode = newRootNode;
        newRootNode.Selected = true;
    }

    protected void button_AddSubCategory_Click(object sender, EventArgs e)
    {
        //get current selected node.
        //
        if (this.treeView_category.SelectedNode == null)
        {
            SABLL.ShowInfoMsg(this, "请选择父节点!");
            return;
        }

        RadTreeNode parentNode = this.treeView_category.SelectedNode;

        //add to UI.
        Guid newCateId = Guid.NewGuid();
        RadTreeNode newRootNode = new RadTreeNode();
        newRootNode.Text = "新根节点";
        parentNode.Nodes.Add(newRootNode);

        //add to database.
        CategoryInfo newCateInfo = new CategoryInfo();
        newCateInfo.Name = newRootNode.Text;
        newCateInfo.Id = newCateId;
        newCateInfo.ParentId = new Guid( parentNode.Value);
        newCateInfo.Idx = newRootNode.Index;

        newRootNode.Value = newCateInfo.Id.ToString();
        SABLL.AddCategory(newCateInfo);

        //begin edit
        //newRootNode.BeginEdit();
        //this.treeView_category.Refresh();
        //this.treeView_category.SelectedNode = newRootNode;
    }

    protected void EditCategoryFrm_Load()
    {
        if (treeView_category == null)
        {
            return;
        }
        //this.treeView_category.HideSelection = false;
        //this.treeView_category.ImageList = GetCategoryImageList();
        //this.treeView_category.ImageIndex = 0;
        //this.treeView_category.SelectedImageIndex = 1;

        //this.treeView_category.AllowDrop = true;
        this.treeView_category.Nodes.Clear();

        
        List<CategoryInfo> cateInfos = SABLL.GetRootCategories();

        if (cateInfos == null)
        {
            return;
        }

        for (int i = 0; i < cateInfos.Count; i++)
        {
            CategoryInfo curCateInfo = cateInfos[i];

            AddNodeByCategoryInfo(this.treeView_category.Nodes, curCateInfo);
        }

        this.treeView_category.ExpandAllNodes();
    }

    private static void AddNodeByCategoryInfo(RadTreeNodeCollection nodes, CategoryInfo cateInfo)
    {
        //add this node.
        RadTreeNode newNode = new RadTreeNode();
        newNode.Text = cateInfo.Name;
        newNode.Value = cateInfo.Id.ToString();
        nodes.Add(newNode);

        //add sub node.
        //get sub nodes.
        List<CategoryInfo> cateInfos = SABLL.GetSubCategories(cateInfo);

        if (cateInfos == null)
        {
            return;
        }

        foreach (CategoryInfo tempCateInfo in cateInfos)
        {
            AddNodeByCategoryInfo(newNode.Nodes, tempCateInfo);
        }
    }

    protected void button_Delete_Click(object sender, EventArgs e)
    {
        RadTreeNode curNode = this.treeView_category.SelectedNode;

        if (curNode == null) return;

        if (curNode.Nodes.Count > 0)
        {
            SABLL.ShowInfoMsg(this, "分类包含子分类，无法删除！");
            return;
        }

        Guid cateId = new Guid(curNode.Value);

        //TODO: check whether it has families.
        List<FamilyInfo> famInfos = SABLL.GetFamiliesByCategoryId(cateId);
        if (famInfos != null && famInfos.Count > 0)
        {
            SABLL.ShowInfoMsg(this, String.Format("分类[{0}]包含{1}个零部件，无法删除！", curNode.Text, famInfos.Count));
            return;
        }

        //
        bool result = SABLL.DeleteCategory(cateId);
        if (!result)
        {
            SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
        }
        //now remove the treenode.
        curNode.Remove();
        //this.treeView_category.Refresh();
    }

    protected void button_Edit_Click(object sender, EventArgs e)
    {
        RadTreeNode curNode = this.treeView_category.SelectedNode;

        //curNode.BeginEdit();
    }

    protected void treeView_category_NodeEdit(object sender, RadTreeNodeEditEventArgs e)
    {
        RadTreeNode curNode = e.Node;
        if (curNode.Value == null) return;

        Guid cateId = new Guid(curNode.Value);
        CategoryInfo updateCategoryInfo = SABLL.GetCategoryInfoByCateId(cateId);

        if (updateCategoryInfo == null)
        {
            SABLL.ShowErrMsg(this, "获取类别失败!" + SABLL.GetLastErrMsg());
            return;
        }

        if (e.Text != null)
        {
            updateCategoryInfo.Name = e.Text;
        }

        bool result = SABLL.UpdateCategory(updateCategoryInfo);
        if (!result)
        {
            SABLL.ShowErrMsg(this, "更新失败!" + SABLL.GetLastErrMsg());
            return;
        }
        curNode.Text = updateCategoryInfo.Name;
    }
    protected void treeView_category_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
    {
        RadTreeNode sourceNode = e.SourceDragNode;
        RadTreeNode destNode = e.DestDragNode;
        RadTreeViewDropPosition dropPosition = e.DropPosition;

        if (sourceNode.TreeView.SelectedNodes.Count <= 1)
        {
            PerformDragAndDrop(dropPosition, sourceNode, destNode);
        }
        else if (sourceNode.TreeView.SelectedNodes.Count > 1)
        {
            foreach (RadTreeNode node in sourceNode.TreeView.SelectedNodes)
            {
                PerformDragAndDrop(dropPosition, node, destNode);
            }
        }
        destNode.Expanded = true;
        //sourceNode.TreeView.ClearSelectedNodes();
    }

    private  void PerformDragAndDrop(RadTreeViewDropPosition dropPosition, RadTreeNode sourceNode,
     RadTreeNode destNode)
    {
        if (sourceNode.Equals(destNode) || sourceNode.IsAncestorOf(destNode))
        {
            return;
        }
        sourceNode.Owner.Nodes.Remove(sourceNode);

        switch (dropPosition)
        {
            case RadTreeViewDropPosition.Over:
                // child
                if (!sourceNode.IsAncestorOf(destNode))
                {
                    destNode.Nodes.Add(sourceNode);
                }
                break;

            case RadTreeViewDropPosition.Above:
                // sibling - above                    
                destNode.InsertBefore(sourceNode);
                break;

            case RadTreeViewDropPosition.Below:
                // sibling - below
                destNode.InsertAfter(sourceNode);
                break;
        }

        //update database.
        CategoryInfo curCateInfo = SABLL.GetCategoryInfoByCateId(new Guid( sourceNode.Value));

        if (curCateInfo != null)
        {
            CategoryInfo curParentCateInfo = SABLL.GetCategoryInfoByCateId(new Guid(destNode.Value));
            if (curParentCateInfo != null)
            {
                curCateInfo.ParentId = curParentCateInfo.Id;
                curCateInfo.Idx = sourceNode.Index;

                if (!SABLL.UpdateCategory(curCateInfo))
                    SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
            }
        }
    }

    protected void button_Up_Click(object sender, EventArgs e)
    {
        RadTreeNode selectedNode = this.treeView_category.SelectedNode;

        if (selectedNode == null) return;

        RadTreeNode preNode = selectedNode.Prev;
        bool bExpand = selectedNode.Expanded;

        if (preNode != null)
        {
            RadTreeNode newNode = (RadTreeNode)selectedNode.Clone();

            RadTreeNodeCollection parentNodes;
            //do the change.
            if (selectedNode.Parent == null)
            {
                parentNodes = this.treeView_category.Nodes;
            }
            else
            {
                parentNodes = selectedNode.ParentNode.Nodes;
            }

            //change start.
            parentNodes.Insert(preNode.Index, newNode);
            selectedNode.Remove();

            //update database for category node index.
            foreach (RadTreeNode node in parentNodes)
            {
                CategoryInfo curCateInfo = SABLL.GetCategoryInfoByCateId(new Guid(node.Value));
                if (curCateInfo != null)
                {
                    curCateInfo.Idx = node.Index;
                    if (!SABLL.UpdateCategory(curCateInfo))
                        SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
                }
            }
            //change end

            if (bExpand) newNode.ExpandChildNodes();
            newNode.Selected = true;
        }
    }

    protected void button_Down_Click(object sender, EventArgs e)
    {
        RadTreeNode selectedNode = this.treeView_category.SelectedNode;

        if (selectedNode == null) return;

        RadTreeNode nextNode = selectedNode.Next;
        bool bExpand = selectedNode.Expanded;

        if (nextNode != null)
        {
            RadTreeNode newNode = (RadTreeNode)selectedNode.Clone();
            RadTreeNodeCollection parentNodes;
            //do the change
            if (selectedNode.Parent == null)
            {
                parentNodes = this.treeView_category.Nodes;
            }
            else
            {
                parentNodes = selectedNode.ParentNode.Nodes;
            }
            parentNodes.Insert(nextNode.Index + 1, newNode);
            selectedNode.Remove();
            //change end.

            //update database for category node index.
            foreach (RadTreeNode node in parentNodes)
            {
                CategoryInfo curCateInfo = SABLL.GetCategoryInfoByCateId(new Guid(node.Value));
                if (curCateInfo != null)
                {
                    curCateInfo.Idx = node.Index;
                    if (!SABLL.UpdateCategory(curCateInfo))
                        SABLL.ShowErrMsg(this, SABLL.GetLastErrMsg());
                }
            }

            if (bExpand) newNode.ExpandParentNodes();
            newNode.Selected = true;
        }
    }
}